package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.maiji.cloud.entities.shopingmall.RuleFundIntegral;
import com.maiji.cloud.entities.shopingmall.RuleFundIntegral_log;
import com.maiji.cloud.mapper.RuleFundIntegralMapper;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.ResMetaData;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.RuleFundIntegralService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RuleFundIntegralServiceImpl extends ServiceImpl<RuleFundIntegralMapper, RuleFundIntegral> implements RuleFundIntegralService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public BaseMetaResDto<List<RuleFundIntegral>> findAllRuleFundIntegrals(Integer type, ReqMetaData metaData) {
        EntityWrapper<RuleFundIntegral> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("type", type);
        entityWrapper.orderAsc(Arrays.asList("purchase_amount"));
        Page<RuleFundIntegral> page = selectPage(new Page<>(metaData.getPage(), metaData.getSize()), entityWrapper);
        ResMetaData resMetaData = new ResMetaData(page.getCurrent(), page.getSize(), page.getTotal());
        return new BaseMetaResDto<List<RuleFundIntegral>>(Status.SUCCESS).setData(page.getRecords()).setResMetaData(resMetaData);
    }

    @Override
    public BaseResDto editRuleFundIntegral(RuleFundIntegral_log rule_log) {
        rule_log.setEditDate(new Date());
        RuleFundIntegral rule = new RuleFundIntegral();
        BeanUtils.copyProperties(rule_log, rule);
        if ("删除规则".equals(rule_log.getOperationDesc())) {
            if (! deleteById(rule.getUuId()))return new BaseResDto(Status.ERROR);
            mongoTemplate.save(rule_log.setOperationDesc("删除规则"), RuleFundIntegral_log.TABLE_NAME);
            return new BaseResDto(Status.SUCCESS);
        }
        if (StringUtil.isBlank(rule.getUuId())) {
            if (! insert(rule.setUuId(UUID_MD5.getUUID()))) return new BaseResDto(Status.ERROR);
            mongoTemplate.save(rule_log.setOperationDesc("添加规则"), RuleFundIntegral_log.TABLE_NAME);
        } else {
            if (! updateById(rule)) return new BaseResDto(Status.ERROR);
            mongoTemplate.save(rule_log.setOperationDesc("修改规则"), RuleFundIntegral_log.TABLE_NAME);
        }
        return new BaseResDto(Status.SUCCESS);
    }

    @Override
    public BaseMetaResDto<List<RuleFundIntegral_log>> findAllRuleFundIntegral_logs (Integer type, String operationDesc, List<Date> updateDates, ReqMetaData metaData) {
        Query query = new Query(Criteria.where("type").is(type));
        if (StringUtil.isNotBlank(operationDesc)) query.addCriteria(Criteria.where("operationDesc").is(operationDesc));
        query.with(new Sort(new Sort.Order(Sort.Direction.DESC,"editDate")));
        if (updateDates != null) {
            Date startDate = updateDates.size() > 0 ? updateDates.get(0): null;
            Criteria updateDateCriteria = Criteria.where("editDate");
            Boolean updFlag = false;
            if (startDate != null) {
                updateDateCriteria = updateDateCriteria.gte(startDate);
                updFlag = true;
            }
            Date endDate = updateDates.size() > 1 ? updateDates.get(1): null;
            if (endDate != null) {
                updateDateCriteria = updateDateCriteria.lte(endDate);
                updFlag = true;
            }
            if (updFlag) query.addCriteria(updateDateCriteria);
        }
        Long total = mongoTemplate.count(query, RuleFundIntegral_log.TABLE_NAME);
        query.skip((metaData.getPage() - 1) * metaData.getSize()).limit(metaData.getSize());
        List<RuleFundIntegral_log> shareOrderRules = mongoTemplate.find(query, RuleFundIntegral_log.class, RuleFundIntegral_log.TABLE_NAME);
        ResMetaData resMetaData = new ResMetaData(metaData.getPage(), metaData.getSize(), total);
        return new BaseMetaResDto<List<RuleFundIntegral_log>>(Status.SUCCESS).setData(shareOrderRules).setResMetaData(resMetaData);
    }

}
